<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LunboController extends Controller
{

    //列表页         
    public function getIndex(Request $request){

        //获取搜索id
        $search = $request->input('search');

        //获取分页id
        $num = $request->input('num',10);

        //判断搜索  模糊查询
        if(!empty($search)){
            $data = DB::table('lunbo')->where('lu_id','like','%'.$search.'%')->paginate($num);
        }else{
             $data = DB::table('lunbo')->paginate($num);
        }

        //引入模板  传输数据
        return view('admin.lunbo.index',['data'=>$data,'request'=>$request->all()]);
    }

  
    //添加页
    public function getAdd(){
        return view('admin.lunbo.add');
    }

    //轮播图插入操作
    public function postInsert(Request $request){
        //压入空数组
        $arr = array();

        //去除_token
        $arr = $request->except('_token');

        //验证数据
        $this->validate($request,[
            'lu_pic'=>'required',
            'lu_url'=>'required',
            ],[
            'lu_pic.required'=>'轮播图片必填',
            'lu_url.required'=>'轮播地址必填',
            ]);
        
        //获取文件后缀
        $hz = $request->file('lu_pic')->getClientOriginalExtension();

        //文件名
        $temp_name = md5(time()+rand(11111,999999));

        //拼接
        $name = $temp_name.'.'.$hz;
        $arr['lu_pic']='/upload/lunbo/'.$name;
        if(file_exists('.'.$arr['lu_pic'])){
            unlink('.'.$arr['lu_pic']);
        }

        //移动
        $request->file('lu_pic')->move('./upload/lunbo',$name);
        $res = DB::table('lunbo')->insert($arr);

        //判断结果
        if($res){
            return redirect('/admin/lunbo/index')->with('success','插入成功');
        }else{
            return back()->with('error','插入失败');
        }
    }

        //修改页
    public function getEdit(Request $request){
        
        //获取修改id
        $res = $request->input('id');

        //获取修改的数据
        $data = DB::table('lunbo')->where('lu_id',$res)->get();
        
        //引入模板传输数据
        return view('admin.lunbo.edit',['data'=>$data[0]]);
    }

    //修改操作
    public function postUpdate(Request $request){

        //获取修改id
        $res = $request->input('id');

        //创建空数组
        $res1 = array();

        //去除_token id
        $res1 = $request->except('_token','id');

        //id转换
        $res1['lu_id'] = $res; 

         //获取文件后缀
        $hz = $request->file('lu_pic')->getClientOriginalExtension();

        //文件名
        $temp_name = md5(time()+rand(11111,999999));

        //拼接
        $name = $temp_name.'.'.$hz;

        $res1['lu_pic']='/upload/lunbo/'.$name;

        if(file_exists('.'.$res1['lu_pic'])){
            unlink('.'.$res1['lu_pic']);
        }

        //移动
        $request->file('lu_pic')->move('./upload/lunbo',$name);

        //获取修改得数据
        $data = DB::table('lunbo')->where('lu_id',$res)->update($res1);

        //判断结果
        if($data>0){
            return redirect('/admin/lunbo/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    //删除操作
    public function getDelete(Request $request){

        //获取删除id
        $res = $request->input('id');

        //获取删除的数据
        $data = DB::table('lunbo')->where('lu_id',$res)->delete();

        //判断结果
        if($data>0){
            return redirect('/admin/lunbo/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    //上线操作
    public function getUp(Request $request){

        //获取上线id
        $res = $request->input('id');

        //获取数据
        $data = DB::table('lunbo')->where('lu_id',$res)->get();
        
        //判断状态   是否上线
        if($data[0]['lu_status']==1){
            $res = DB::table('lunbo')->where('lu_id',$res)->update(['lu_status'=>2]);
            return back()->with('success','上线成功');
        }else{
            return back()->with('error','上线失败');
        }
        
    }

    //下线操作
    public function getDown(Request $request){

        //获取下线id
        $res = $request->input('id');

        //获取下线数据
        $data = DB::table('lunbo')->where('lu_id',$res)->get();
        
        //判断状态  是否下线
        if($data[0]['lu_status']==2){
            $res = DB::table('lunbo')->where('lu_id',$res)->update(['lu_status'=>1]);
          return back()->with('success','下线成功');
        }else{
            return back()->with('error','下线失败');
        }
        
    }
}

                